package com.app.reading;

import android.os.Handler;
import android.os.Message;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class Main6Activity extends AppCompatActivity {

    private LinearLayout ll_dots;
    private TextView viewpager_tv;


    private ViewPager viewpager_vp;


    ArrayList<ImageView> imageViews = new ArrayList<ImageView>();


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    int currentItem = viewpager_vp.getCurrentItem();

                    viewpager_vp.setCurrentItem(currentItem + 1);

                    sendEmptyMessageDelayed(1, 1000);
                    break;
                default:
                    break;
            }

        }
    };


    private int[] imageResIds = {R.drawable.title, R.drawable.titlee, R.drawable.titleee, R.drawable.titleeee, R.drawable.titleeeee};


    private String[] descs = {
            "学习教育，知史安岗",
            "极端？爱你爱到要杀死你",
            "酸甜的糖果，嘴中的苦涩",
            "姻缘错，红颜误，勿相忆",
            "课读场带你逆袭,助你走向人生巅峰"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        initView();
    }

    private void initView() {
        ll_dots = (LinearLayout) findViewById(R.id.ll_dots);
        viewpager_tv = (TextView) findViewById(R.id.viewpager_tv);
        viewpager_vp = (ViewPager) findViewById(R.id.viewpager_vp);

        for (int x = 0; x < imageResIds.length; x++) {
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(imageResIds[x]);
            imageViews.add(imageView);
            dot();
        }
        viewpager_vp.setAdapter(new Myadapter());
        viewpager_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int currentItem = viewpager_vp.getCurrentItem();
                changeTextAndDot(currentItem % imageResIds.length);
                Log.d("aaa", "onPageScrolled: Position-" + position + " positionOffset-" + positionOffset + " positionOffsetPixels-" + positionOffsetPixels);

            }

            @Override
            public void onPageSelected(int position) {
                Log.d("aaa", "onPageSelected: position" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.d("aaa", "onPageScrollStateChanged: state" + state);
            }
        });
        viewpager_vp.setCurrentItem(Integer.MAX_VALUE / 2 - 3);
        handler.sendEmptyMessageDelayed(1, 1000);
        ViewPagerTouchEvent();
    }

    private class Myadapter extends PagerAdapter {
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = imageViews.get(position % imageResIds.length);
            container.addView(imageView);
            return imageView;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }


    private void dot() {
        View view = new View(this);
        view.setBackgroundResource(R.drawable.bg_main);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(8, 8);
        layoutParams.leftMargin = 8;
        view.setLayoutParams(layoutParams);
        ll_dots.addView(view);
    }

    public void changeTextAndDot(int position) {
        viewpager_tv.setText(descs[position]);
        for (int x = 0; x < imageResIds.length; x++) {
            View childAt = ll_dots.getChildAt(x);
            childAt.setBackgroundResource(x == position ? R.drawable.title: R.drawable.titleeeee);
        }
    }

    public void ViewPagerTouchEvent() {
        viewpager_vp.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        handler.removeMessages(1);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        handler.removeMessages(1);
                        break;
                    case MotionEvent.ACTION_UP:
                        handler.sendEmptyMessageDelayed(1, 3000);
                    default:
                        break;
                }
                return false;
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(1);
    }
}
